package cn.itcast.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CreateLucene {
    /**
     * 测试创建索引
     */
    @Test
    public void testCreate() throws IOException {
        // 创建文档数据
        Document document = new Document();

        // 添加字段，参数Field是一个接口，要new实现类的对象(StringField, TextField)
        // StringField的实例化需要3个参数：1-字段名，2-字段值，3-是否保存到文档，Store.YES存储，NO不存储
        document.add(new StringField("id", "1", Field.Store.YES));

        // TextField：创建索引并提供分词，StringField创建索引但不分词
        document.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Field.Store.YES));

        // 创建目录对象，指定索引库的存放位置；FSDirectory文件系统；RAMDirectory内存
        Directory directory = FSDirectory.open(new File("D:\\tmp\\indexDir"));

        // 创建分词器对象
        Analyzer analyzer = new StandardAnalyzer();

        // 创建索引写入器配置对象，第一个参数版本VerSion.LATEST,第一个参数分词器
        IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);

        // 创建索引写入器
        IndexWriter indexWriter = new IndexWriter(directory , conf);

        // 向索引库写入文档对象
        indexWriter.addDocument(document);

        // 提交
        indexWriter.commit();

        // 关闭
        indexWriter.close();
    }

    @Test
    public void testCreate2() throws IOException {
        // 创建文档对象集合
        List<Document> docs = new ArrayList<>();

        // 创建文档对象
        Document document1 = new Document();
        TextField field = new TextField("title", "谷歌地图之父跳槽FaceBook，加入了传智播客，简直碉堡了啊", Field.Store.YES);
        field.setBoost(100f);
        document1.add(new StringField("id", "1", Field.Store.YES));
        document1.add(field);
        document1.add(new LongField("price", 100000l, Field.Store.YES));
        docs.add(document1);
        // 创建文档对象
        Document document2 = new Document();
        document2.add(new StringField("id", "2", Field.Store.YES));
        document2.add(new TextField("title", "谷歌地图之父加盟FaceBook", Field.Store.YES));
        document2.add(new LongField("price", 200000l, Field.Store.YES));
        docs.add(document2);
        // 创建文档对象
        Document document3 = new Document();
        document3.add(new StringField("id", "3", Field.Store.YES));
        document3.add(new TextField("title", "谷歌地图创始人拉斯离开谷歌加盟Facebook", Field.Store.YES));
        document3.add(new LongField("price", 300000l, Field.Store.YES));
        docs.add(document3);
        // 创建文档对象
        Document document4 = new Document();
        document4.add(new StringField("id", "4", Field.Store.YES));
        document4.add(new TextField("title", "谷歌地图之父跳槽Facebook与Wave项目取消有关", Field.Store.YES));
        document4.add(new LongField("price", 400000l, Field.Store.YES));
        docs.add(document4);
        // 创建文档对象
        Document document5 = new Document();
        document5.add(new StringField("id", "5", Field.Store.YES));
        document5.add(new TextField("title", "谷歌地图之父拉斯加盟社交网站Facebook", Field.Store.YES));
        document5.add(new LongField("price", 500000l, Field.Store.YES));
        docs.add(document5);

        // 索引库对象
        FSDirectory directory = FSDirectory.open(new File("D:\\tmp\\indexDir"));

        // 创建索引写入器配置对象，1-版本，2-分词器：标准分词器
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());

        // OpenMode打开模式，枚举类，CREATE：覆盖，APPEND：追加，
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

        // 创建索引写入器对象
        IndexWriter indexWriter = new IndexWriter(directory, config);

        // 执行写入操作
        indexWriter.addDocuments(docs);

        // 提交
        indexWriter.commit();

        // 关闭
        indexWriter.close();


    }
}
